package com.yjhb.android.sbz.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.TextView;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.dialog.TipsDialog;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.mvp.myinviter.MyInviterPresenter;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.mvp.CommonView;
import com.jojo.android.zxlib.tool.ZxNetTool;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/7 16:16
 */
public class MyInviterActivity extends BaseActivity<MyInviterPresenter> implements CommonView<Object> {
    private final String TAG = "MyInviterActivity";
    @BindView(R.id.ed_modify)
    EditText edModify;
    @BindView(R.id.save_btn)
    TextView save_btn;

    @Override
    protected MyInviterPresenter initPresenter() {
        return new MyInviterPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_inviter;
    }

    @Override
    protected void initUI() {
        edModify.setHint(ZxSpanTool.ZoomSpan("请输入邀请人邀请码", 0, 9, 0.7f));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {

    }

    @Override
    public void onEventMsg(EventMsg msg) {
        super.onEventMsg(msg);
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            refresh();
        }
    }

    @Override
    public void refresh() {
        if (App.userInfo.getParentId() != null && App.userInfo.getParentId() != 0) {
            edModify.setText("" + App.userInfo.getParentId());
            edModify.setEnabled(false);
            save_btn.setEnabled(false);
            save_btn.setText("已绑定");
        } else {
            edModify.setEnabled(true);
            edModify.setText("");
            save_btn.setEnabled(true);
            save_btn.setText("保存");
        }
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick(R.id.save_btn)
    void onSave() {
        final String vid = Tool.getText(edModify);
        if (TextUtils.isEmpty(vid)) {
            ZxToast.showINFOBottomToast("请先输入邀请人ID哦！");
            return;
        }
        TipsDialog tipsDialog = new TipsDialog(this);
        tipsDialog.setTitle("提示");
        tipsDialog.setContent("绑定邀请人后不可更改，\n确认绑定？");
        tipsDialog.setSubmitText("确定");
        tipsDialog.setCancelText("取消");
        tipsDialog.setContentGravity(Gravity.CENTER);
        tipsDialog.setOnClickListener(new TipsDialog.OnClickListener() {
            @Override
            public void onClick(int type) {
                tipsDialog.cancel();
                if (type == TipsDialog.TYPE_SUBMIT) {
                    try {
                        presenter.bindMyInviter(Integer.parseInt(vid));
                    } catch (Exception e) {
                        ZxToast.showINFOBottomToast("请输入正确的邀请人ID！");
                    }
                }
            }
        });
        tipsDialog.show();
    }

    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onSuccess(Object data) {
        loadingDialog.cancel();
        ZxToast.showSuccessBottomToast("绑定成功！");
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (!ZxNetTool.isNetworkAvailable(this)) {
            ZxToast.showCenterToastLight("网络连接异常~~");
            return;
        }
        if (code != -1) {
            ZxToast.showINFOBottomToast(msg);
        }
    }
}
